package cn.geminis.warehouse.service.purchase.entity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import jakarta.persistence.AttributeConverter;
import lombok.Getter;

@Getter
public enum PurchaseState {

    PLAN(10, "已计划"),
    DELIVERY(20, "已发货"),
    ARRIVE(30, "已到货"),
    RECEIVE(40, "已收货"),
    CHECK(50, "已验收"),
    PACKAGE(60, "已装箱");


    @JsonValue
    private final int value;
    private final String name;

    PurchaseState(int value, String name) {
        this.value = value;
        this.name = name;
    }

    @JsonCreator
    public static PurchaseState fromValue(int value) {
        for (var orderInState : PurchaseState.values()) {
            if (orderInState.getValue() == value) {
                return orderInState;
            }
        }
        throw new IllegalArgumentException();
    }

    public static class PurchaseStateJpaConverter implements AttributeConverter<PurchaseState, Integer> {

        @Override
        public Integer convertToDatabaseColumn(PurchaseState state) {
            return state.getValue();
        }

        @Override
        public PurchaseState convertToEntityAttribute(Integer value) {
            return PurchaseState.fromValue(value);
        }
    }

}
